package starComponent.Listeners;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.SwingUtilities;

import starComponent.elements.Thumb;
import tuio.AcceleratedTouchEvent;

/**
 * @author Charles Perin
 * 21 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class ListenerThumb implements MouseListener, MouseMotionListener{

	private Thumb thumb;

	public ListenerThumb(Thumb thumb){
		this.thumb = thumb;
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		if(e instanceof AcceleratedTouchEvent){//TODO - snap contextuel
			AcceleratedTouchEvent ate = (AcceleratedTouchEvent)e;
			if(thumb.isSelected()){
				//System.out.println("motion speed = "+ate.getMotionSpeed());
				MouseEvent convertedMouse = SwingUtilities.convertMouseEvent(thumb, ate, thumb.getParent());
				thumb.checkNewPosition(convertedMouse.getX(), convertedMouse.getY());
			}
		}
		else{
			if(thumb.isSelected()){
				MouseEvent convertedMouse = SwingUtilities.convertMouseEvent(thumb, e, thumb.getParent());
				thumb.checkNewPosition(convertedMouse.getX(), convertedMouse.getY());
			}
		}
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {

	}

	@Override
	public void mouseClicked(MouseEvent arg0) {

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {

	}

	@Override
	public void mouseExited(MouseEvent arg0) {

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		thumb.setSelected(true);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if(thumb.isSelected()){
			MouseEvent convertedMouse = SwingUtilities.convertMouseEvent(thumb, e, thumb.getParent());
			thumb.checkNewPosition(convertedMouse.getX(), convertedMouse.getY());
		}
		thumb.setSelected(false);
	}

}
